package com.google.gwt.sample.json.client;
import org.gwtwidgets.client.util.WindowUtils;

import com.google.gwt.json.client.JSONArray;
import com.google.gwt.json.client.JSONException;
import com.google.gwt.json.client.JSONParser;
import com.google.gwt.json.client.JSONValue;
import com.google.gwt.sample.json.client.Header.JSONResponseTextHandler;
import com.google.gwt.user.client.HTTPRequest;
import com.google.gwt.user.client.ResponseTextHandler;
import com.google.gwt.user.client.ui.*;
import com.google.gwt.user.client.Window;

abstract class SearchResult extends Composite{
	static public class JSONCountResponseTextHandler implements ResponseTextHandler {
		public void onCompletion(String responseText) {
			try {
				JSONValue jsonValue = JSONParser.parse(responseText);
				if (jsonValue != null){
					count =  jsonValue.isObject().get("count").toString().replaceAll("\"", "");
				}
			} catch (JSONException e) {
			}
		}
	}

	class SearchResultClickListener implements ClickListener {
		public SearchResultClickListener(){
		    tb.addKeyboardListener(new KeyboardListenerAdapter() {
		        public void onKeyPress(Widget sender, char keyCode, int modifiers) {
		          if (keyCode == (char) KEY_ENTER){
		        	  doSearch(tb.getText(), true);
		          }
		        }
		      });
	    };
		public void onClick(Widget sender) {
      	  doSearch(tb.getText(), true);
		}
	}
	
	public class SearchResultMouseListener implements MouseListener{
		private Label label;
		private JSONValue json;
		public SearchResultMouseListener(Label label, JSONValue json){
			this.label = label;
			this.json = json;
		}
		public void onMouseDown(final Widget sender, final int x, final int y){
			EntryPanel entry = makeEntryPanel();
			entry.updateEntry(json);
			MainPage.setMainPanel(entry, true); 
		}
		public void onMouseEnter(final Widget sender) {
			label.setStyleName("gwt-Result-Over");		   
		}
		public void onMouseLeave(final Widget sender) {
			label.setStyleName("gwt-Result");		   
		}
		public void onMouseMove(final Widget sender, final int x, final int y) {}
		public void onMouseUp(final Widget sender, final int x, final int y) {}
	}
	class NextClickListener implements ClickListener{
		public void onClick(Widget sender) {
			offset = offset + 10;
      	  	doSearch(tb.getText(), false);
		}			
	}
	class LastClickListener implements ClickListener{
		public void onClick(Widget sender) {
			offset = offset - 10;
      	  	doSearch(tb.getText(), false);
		}			
	}

	final VerticalPanel resultPanel = new VerticalPanel();
	protected int offset = 0;
	static protected String count;
	private TextBox tb;
	public SearchResult(boolean searchWithin) {
		final VerticalPanel verticalPanel_1 = new VerticalPanel();
		verticalPanel_1.setWidth("700px");
		initWidget(verticalPanel_1);
		
		final HorizontalPanel horizontalPanel = new HorizontalPanel();
		final HorizontalPanel SearchWithinPanel = new HorizontalPanel();
		SearchWithinPanel.setStyleName("gwt-SearchLabel");
		final Label searchResultsLabel = new Label(" Search Results for " + entryName());
		verticalPanel_1.add(horizontalPanel);
		horizontalPanel.add(searchResultsLabel);
		
		if(searchWithin){
			horizontalPanel.add(SearchWithinPanel);
	
			searchResultsLabel.setSize("400px", "100%");
			searchResultsLabel.setStyleName("gwt-SearchResults");
			SearchWithinPanel.setSize("300px", "50px");
	
			final VerticalPanel verticalPanel2 = new VerticalPanel();
			SearchWithinPanel.add(verticalPanel2);		
			final HorizontalPanel horizontalPanel3 = new HorizontalPanel();
			tb = new TextBox();
			tb.setSize("100%", "25px");
			tb.setName("content");
			horizontalPanel3.setSize("300px", "30px");
	
			horizontalPanel3.add(tb);
			horizontalPanel3.setHorizontalAlignment(HasHorizontalAlignment.ALIGN_RIGHT);
			horizontalPanel3.setCellHorizontalAlignment(tb, HasHorizontalAlignment.ALIGN_RIGHT);
	
			final Label label2 = new Label("Search within " + entryName());
			label2.setStyleName("gwt-SearchLabel");
			label2.setHeight("25px");
			verticalPanel2.add(label2);
			verticalPanel2.add(horizontalPanel3);
			
			final Button button = new Button("searchy", new SearchResultClickListener());
			horizontalPanel3.add(button);
			button.setHeight("25px");
		}
		else{
			searchResultsLabel.setSize("700px", "100%");
		}
		verticalPanel_1.add(resultPanel);
		resultPanel.setSize("100%", "100%");

		final HorizontalPanel hp_1 = new HorizontalPanel();
		final Label result = new Label("First Result");
		final Image im_1 = new Image("home.gif");
		im_1.setHeight("40px");
		hp_1.add(result);
		hp_1.add(im_1);
		resultPanel.add(hp_1);
		result.setHeight("40px");
		result.setStyleName("gwt-Result");

	}
	public void setLabels(JSONArray jsonArray, String query){
		resultPanel.clear();
		String countStr = Integer.toString(1+offset) + " to " + Integer.toString(jsonArray.size()+offset) + " of " +  count;
    	String searchText = " Search Results";
    	if(query != "")
    		searchText = searchText + " for " + query;
        final Label result = new Label(countStr + searchText);
		resultPanel.add(result);
		result.setHeight("25px");
		result.setStyleName("gwt-Result");		        	

		final HorizontalPanel searchLabelsPanel = new HorizontalPanel();
		final Label nextLabel = new Label("next");
		final Label prevLabel = new Label("last");
		nextLabel.addClickListener(new NextClickListener());
		prevLabel.addClickListener(new LastClickListener());
		searchLabelsPanel.add(nextLabel);
		searchLabelsPanel.add(prevLabel);
		resultPanel.add(searchLabelsPanel);
		searchLabelsPanel.setHeight("25px");
		searchLabelsPanel.setStyleName("gwt-Result");		        	

		for (int i = 0; i < jsonArray.size(); ++i) {
        	resultPanel.add(makeEntryLabel(jsonArray.get(i)));
        }
	}
	public abstract Widget makeEntryLabel(JSONValue json);
	public abstract EntryPanel makeEntryPanel();
	public abstract String entryName();
	public abstract void doSearch(String tbText, boolean findCount);
}
